package com.webwork.lims.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.system.dataobject.Menu;
import com.webwork.lims.system.service.MenuService;

@Controller
@RequestMapping(value = "/system")
public class MenuController extends SuperCtrl {

	@Autowired
	private MenuService menuSve;
	
	@RequestMapping(value = "/menus")
	@Override
	public String mainPage() {
		return "menus";
	}
	
	@RequestMapping(value = "/getMenu")
	@ResponseBody
	public ResponseEntity<String> getMenu(@RequestParam("menuCode") String menuCode) throws Exception {
		Menu menu = menuSve.get(menuCode);
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		return new ResponseEntity<String>(new Gson().toJson(menu).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "/getMenuList")
	@ResponseBody
	public ResponseEntity<String> getMenuList(String parentCode) throws Exception {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		List<Menu> ret = new ArrayList<Menu>();
		Menu queryObj = new Menu();
		if (!parentCode.equals("0")) {
			queryObj.setParentCode(parentCode);
		}
		List<Menu> list = menuSve.query(queryObj);
		if (!parentCode.equals("0")) {
			for (Menu menu : list) {
				if (!menu.getMenuCode().equals(menu.getParentCode())) {
					ret.add(menu);
				}
			}
		} else {
			for (Menu menu : list) {
				if (menu.getMenuCode().equals(menu.getParentCode())) {
					ret.add(menu);
				}
			}
		}
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("total", ret.size());
		result.put("rows", ret);
		return new ResponseEntity<String>(new Gson().toJson(result).toString(), responseHeaders, HttpStatus.CREATED);
	}

	@RequestMapping(value="loadParent")
	@ResponseBody
	public ResponseEntity<String> loadParent() {
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		List<Menu> menuList = new ArrayList<Menu>();
		List<Menu> menus = menuSve.query(new Menu());
		for (Menu menu : menus) {
			if (menu.getMenuCode().equals(menu.getParentCode())) {
				menuList.add(menu);
			}
		}		
		List<Object> ret = new ArrayList<Object>();
		Map<String, Object> allMap = new HashMap<String, Object>();
		allMap.put("value", 0);
		allMap.put("name", "全部");
		ret.add(allMap);
		for (Menu o : menuList) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("value", o.getMenuCode());
			map.put("name", o.getMenuName());
			ret.add(map);
		}
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
	@RequestMapping(value = "/addMenu")
	@ResponseBody
	public String addMenu(@RequestParam("menu") String menuJsonString) throws Exception {
		Menu menu = new Gson().fromJson(menuJsonString, Menu.class);
		if (menuSve.get(menu.getMenuCode()) != null) {
			return EXIST;
		}
		if (menu.getParentCode().equals("0")) {
			menu.setParentCode(menu.getMenuCode());
		}
		menuSve.save(menu);
		return SUCCESS;
	}

	@RequestMapping(value = "/editMenu")
	@ResponseBody
	public String editMenu(@RequestParam("menu") String menuJsonString) throws Exception {
		Menu menu = new Gson().fromJson(menuJsonString, Menu.class);
		if (menuSve.get(menu.getMenuCode()) == null) {
			return NOT_EXIST;
		}
		menuSve.save(menu);
		return SUCCESS;
	}

	@RequestMapping(value = "/delMenu")
	@ResponseBody
	public String deleteMenu(@RequestParam("menuCode") String menuCode) throws Exception {
		menuSve.delete(menuCode);
		return SUCCESS;
	}
	
}
